package backend

import (
	"github.com/gin-gonic/gin"
	"naio/constant"
	"naio/errs"
	"naio/model"
	"naio/serializer"
	"naio/validate"
	"net/http"
	"strconv"
)

// @Summary 获取所有文章
// @Produce json
// @Success 200 {object} string '{"code":200,"data":{},"msg":"ok"}'
// @Router /backend/article [get]
func (h *Handler) BannerBrowse(c *gin.Context) {
	var _status = c.DefaultQuery("status", "1")
	var _page = c.DefaultQuery("page", "1")
	var _size = c.DefaultQuery("size", "15")
	status, _ := strconv.Atoi(_status)
	page, _ := strconv.Atoi(_page)
	size, _ := strconv.Atoi(_size)
	var cateVOList []constant.BackendBannerResponse
	cateList, total := h.service.BannerService.GetBannerList(status, page, size)
	for _, v := range *cateList {
		cateVO := constant.BackendBannerResponse{}
		cateVO.Id = v.Id
		cateVO.Name = v.Name
		cateVO.Picture = v.Picture
		cateVO.Status = v.Status
		cateVOList = append(cateVOList, cateVO)
	}
	data := make(map[string]interface{})
	data["list"] = cateVOList
	data["total"] = total
	res := serializer.SuccessMessage{Code: 0, Msg: "ok", Data: nil}
	res.Data = data
	res.RequestUrl = c.Request.Method + " " + c.Request.URL.String()
	c.JSON(http.StatusOK, res)
}

// @Summary 获取文章
// @Produce json
// @Success 200 {object} string '{"code":200,"data":{},"msg":"ok"}'
// @Router /backend/article/:id [get]
func (h *Handler) BannerFetch(c *gin.Context)  {
	var _id = c.Param("id")
	if _id == "" {
		panic(errs.NewMyAPIError(errs.ErrorParameterValidate, "id参数必填!!!"))
	}

	id, _ := strconv.Atoi(_id)
	banner := h.service.BannerService.GetBannerById(id)
	res := serializer.SuccessMessage{Code: 0, Msg: "ok", Data: nil}
	res.Data = banner
	res.RequestUrl = c.Request.Method + " " + c.Request.URL.String()
	c.JSON(http.StatusOK, res)
}

// @Summary 创建或更新文章
// @Produce json
// @Success 200 {object} string '{"code":200,"data":{},"msg":"ok"}'
// @Router /backend/article [post]
func (h *Handler) BannerUpdate(c *gin.Context) {
	req := constant.BackendBannerRequest{}
	if err := c.ShouldBindJSON(&req); err != nil {
		panic(errs.NewMyAPIError(errs.InvalidParams, err.Error()))
	}

	validator, _ := validate.Default()
	if check := validator.CheckStruct(req); !check {
		panic(errs.NewMyAPIError(errs.InvalidParams, validator.GetAllErrorString()))
	}

	if req.Id == 0 {
		// add
		banner := &model.BannerModel{
			Picture: req.Picture,
			Name: req.Name,
			BusinessType: req.BusinessType,
			BusinessId: req.BusinessId,
			Status: req.Status,
		}
		h.service.BannerService.AddBanner(banner)
	}else{
		// update
		banner := h.service.BannerService.GetBannerById(req.Id)
		if banner.Del == constant.DELETE {
			panic(errs.NewMyAPIError(errs.ErrorBannerNotExist, "banner不存在"))
		}

		banner.Picture = req.Picture
		banner.Name = req.Name
		banner.BusinessType = req.BusinessType
		banner.BusinessId = req.BusinessId
		banner.Status = req.Status
		h.service.BannerService.UpdateBannerById(banner)
	}

	res := serializer.SuccessMessage{Code: 0, Msg: "ok", Data: nil}
	res.RequestUrl = c.Request.Method + " " + c.Request.URL.String()
	c.JSON(http.StatusOK, res)
}

// @Summary 删除文章
// @Produce json
// @Success 200 {object} string '{"code":200,"data":{},"msg":"ok"}'
// @Router /backend/article/:id [delete]
func (h *Handler) BannerDelete(c *gin.Context) {
	var _id = c.Param("id")
	if _id == "" {
		panic(errs.NewMyAPIError(errs.ErrorParameterValidate, "id参数必填!!!"))
	}

	id, _ := strconv.Atoi(_id)
	banner := h.service.BannerService.GetBannerById(id)
	if banner.Del == constant.DELETE {
		panic(errs.ErrorBannerNotExist)
	}

	banner.Del = constant.DELETE
	h.service.BannerService.UpdateBannerById(banner)
	res := serializer.SuccessMessage{Code: 0, Msg: "ok", Data: nil}
	res.RequestUrl = c.Request.Method + " " + c.Request.URL.String()
	c.JSON(http.StatusOK, res)
}

